/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.gis.eemc.web;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;

import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;

import net.gis.eemc.domain.Faculty;
import net.gis.eemc.domain.Department;
import net.gis.eemc.service.FacultyServiceImpl;
import net.gis.eemc.service.DepartmentServiceImpl;
import net.gis.eemc.service.SpecialityServiceImpl;

/**
 *
 * @author Sergey
 */
@ManagedBean(name = "addSpecialityBean")
@SessionScoped
public class AddSpecialityBean {

    private Integer selectedFaculty = null;
    private Integer selectedDepartment = null;
    private String title = null;
    private String shortName = null;
    private String referenceNumber = null;
    private List<SelectItem> selectFaculties = null;
    private Map<Integer, Faculty> facultyMap = null;
    private List<SelectItem> selectDepartments = null;
    private Map<Integer, Department> departmentMap = null;
    private FacultyServiceImpl facSrv = null;
    private DepartmentServiceImpl depSrv = null;
    private SpecialityServiceImpl specSrv = null;

    private synchronized void fillSelectFaculties() {
        selectFaculties = new ArrayList<SelectItem>();
        facultyMap = new HashMap<Integer, Faculty>();
        selectFaculties.add(new SelectItem(0, new String()));
        for (Faculty fac : facSrv.getFaculties()) {
            facultyMap.put(fac.getIdfaculty(), fac);
            selectFaculties.add(new SelectItem(fac.getIdfaculty(), fac.getShortName()));
        }
    }

    private synchronized void fillSelectDepartments() {
        if (selectedFaculty != null) {
            selectDepartments = new ArrayList<SelectItem>();
            departmentMap = new HashMap<Integer, Department>();

            for (Department dep : depSrv.getDepartmentsForFaculty(selectedFaculty)) {
                departmentMap.put(dep.getIdDep(), dep);
                selectDepartments.add(new SelectItem(dep.getIdDep(), dep.getShortName()));
            }
        } else {
            selectedDepartment = null;
            departmentMap = null;
        }
    }

    /** Creates a new instance of AddSpecialityBean */
    public AddSpecialityBean() {
        facSrv = new FacultyServiceImpl();
        depSrv = new DepartmentServiceImpl();
        specSrv = new SpecialityServiceImpl();

        fillSelectFaculties();
        fillSelectDepartments();
    }

    public void updateDepartmentList() {
        fillSelectDepartments();
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getShortName() {
        return shortName;
    }

    public void setShortName(String shortName) {
        this.shortName = shortName;
    }

    public String getReferenceNumber() {
        return referenceNumber;
    }

    public void setReferenceNumber(String referenceNumber) {
        this.referenceNumber = referenceNumber;
    }

    public Integer getSelectedFaculty() {
        return selectedFaculty;
    }

    public void setSelectedFaculty(Integer faculty) {
        this.selectedFaculty = faculty;
        fillSelectDepartments();
    }

    public Integer getSelectedDepartment() {
        return selectedDepartment;
    }

    public void setSelectedDepartment(Integer department) {
        this.selectedDepartment = department;
    }

    public List<SelectItem> getSelectFaculties() {
        return selectFaculties;
    }

    public List<SelectItem> getSelectDepartments() {
        return selectDepartments;
    }

    public void Save() {
        try {
            specSrv.SaveSpeciality(departmentMap.get(selectedDepartment), title, shortName, referenceNumber);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
